let jwt = require("jsonwebtoken");
let { User } = require("../model/users");

// 登录
const login = async (ctx) => {
  try {
    let { username, password } = ctx.request.body;
    const rel = await User.findOne({
      username,
      password,
    });
    let token = "";
    if (rel) {
      token = jwt.sign(
        {
          username: rel.username,
          _id: rel._id,
        },
        "srf-server-jwt",
        {
          expiresIn: 3600 * 24 * 7,
        }
      );

      return (ctx.body = {
        code: 200,
        msg: "登录成功",
        data: {
          token
        }
      });
    } else {
      return (ctx.body = {
        code: 400,
        msg: "用户名或密码错误",
      });
    }
  } catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });
  }
};

// 注册
const reg = async (ctx) => {
  try {
    let { username, password } = ctx.request.body;
    let isDouble = false;
    // 查找用户名是否存在
    let rel = await User.findOne({ username });
    if (rel) {
      isDouble = true;
    }

    if (isDouble) {
      return (ctx.body = {
        code: 300,
        msg: "用户名已存在",
      });
    }

    let rel2 = await User.create({
      username,
      password,
    });

    if (rel2) {
      return (ctx.body = {
        code: 200,
        msg: "注册成功"
      });
    }
  } catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });
  }
};

// 验证用户登录
const verify = async (ctx) => {
  let token = ctx.header.authorization;
  token = token.replace("Bearer ", '');
  try {
    let result = jwt.verify(token, 'srf-server-jwt')
    let rel = await User.findOne({ _id: result._id})
    if(rel) {
      return ctx.body = {
        code: 200,
        msg: '用户认证成功',
        data: result
      }
    } else {
      return ctx.body = {
        code: 400,
        msg: '用户认证失败',
      }
    }
  } catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });  
  }
}

// 修改密码函数
const updatePwd = async ctx => {
  try {
    const { username, password } = ctx.request.body;
    // 修改需要两个参数 一个是查询条件 一个是修改的内容
    const rel = await User.updateOne({ username }, { password });
    console.log('---------',rel)
    if(rel?.matchedCount >0) {
      return ctx.body = {
        code: 200,
        msg: "修改成功",
      }
    } else {
      return ctx.body = {
        code: 400,
        msg: "修改失败",
      }
    }
  }
  catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });
  }
}

// 获取用户信息
const getUserInfo = async ctx => {
  try {
    const { id } = ctx.query;
    console.log('id', id)
    const rel = await User.findOne({ _id: id });
    
    if (rel) {
      return ctx.body = {
        code: 200,
        msg: "获取成功",
        data: rel
      }
    } else {
      return ctx.body = {
        code: 400,
        msg: "获取失败",
      }
    }
  }
  catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });
  }
}

// 修改用户个人信息
const updatePersonal = async ctx => {
  try {
    let user = ctx.request.body
    let rel = await User.updateOne({
      _id: user._id
    }, {
      avatar: user.avatar,
      sex: user.sex,
      desc: user.desc,
      phone: user.phone,
      email: user.email
    })
    if(rel.modifiedCount > 0) {
      return ctx.body = {
        code: 200,
        msg: "修改成功"
      }
    } else {
      return ctx.body = {
        code: 400,
        msg: "修改失败"
      }
    }
  } catch (error) {
    return (ctx.body = {
      code: 500,
      msg: "服务器错误",
      error,
    });
  }
}

module.exports = {
  login,
  reg,
  verify,
  updatePwd,
  getUserInfo,
  updatePersonal
};
